package com.tsingteng.cosfun.app;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.tsingteng.cosfun.BuildConfig;
import com.tsingteng.cosfun.bean.DBPlayBean;
import com.tsingteng.cosfun.bean.JoinCostarBean;
import com.tsingteng.cosfun.bean.VideoSectionBean;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.http.Url;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.DBUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.SPUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 描述:全局的applition
 * 创建人: 肖利
 *时间: 2018/7/30
 * qq: (872179966)
 */

public class AppContext {
    private static Context mContext;
    private static AppContext mInstance;
    public static String BASE_URL;


    private AppContext(Context mContext) {
        this.mContext = mContext;
        init();
    }
    public static void init() {
        Map<String,Object> map = new HashMap<>();
        if (BuildConfig.DEBUG) {
            BASE_URL = Url.BASE_URL_DEBUG;
        } else {
            BASE_URL = Url.BASE_URL;
        }
        AppUtils.setGid();
        map.put("gid",AppUtils.getGid());//设备唯一ID
        map.put("app_source",AppUtils.getAppMetaData(mContext,"UMENG_CHANNEL"));//渠道
        map.put("plat",2);//平台号
        map.put("sys","android");//客户端操作系统
        map.put("sysver",AppUtils.getAppPhoneSDK());//客户端操作系统版本号
        map.put("sver",String.valueOf(AppUtils.getAppVersion()));//客户端版本号
        map.put(" mfo",AppUtils.getDeviceBrand());//终端生产厂商
        map.put(" mfov",AppUtils.getAppPhoneName());//终端具体机型
        if (LogonUtils.getProFildId()!=0) {
            map.put(" profile_id",SPUtils.init().getInt("profile_id"));
            map.put(" token", LogonUtils.getToken());//token
        }
        LogonUtils.setCookie(mapToString(map));
    }

    public static String mapToString(Map<String,Object>  map) {
        StringBuilder params = new StringBuilder();
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            Object val = map.get(key);
            params.append(key);
            params.append("=");
            params.append(val);
            params.append(";");
        }
       return params.toString();
    }

    public static Context getContext() {
        return mContext;
    }

    public static AppContext getmInstance() {
        return mInstance;
    }

    public static void initAppContext(Context context) {
        if (mInstance==null) {
            synchronized (AppContext.class) {
                if (mInstance==null) {
                    mInstance=new AppContext(context.getApplicationContext());
                }
            }
        }
    }

    /**
     * 判断是否有草稿箱
     * @return ture 有  false 无
     */
    public static boolean isHaveDrafts() {
        List<DBPlayBean> play = DBUtils.selectPlayDB("play");

        if (play!=null && play.size()>0) {
            return true;
        }
        JoinCostarBean lastJoinCostarBean = DBUtils.getLastJoinCostarBean();
        if (lastJoinCostarBean!= null ) {
            return true;
        }
        return false;
    }

    /**
     * 得到草稿箱封面  bitmap为空的时候只有段子,显示默认的图片(注意的是只有确定有草稿箱的时候,才能调用此方法)
     * @return
     */
    public static String getDraftsCover() {
        JoinCostarBean lastJoinCostarBean = DBUtils.getLastJoinCostarBean();
        if (lastJoinCostarBean==null ) return null;

            List<VideoSectionBean> videoSectionInfos = lastJoinCostarBean.getVideoSectionInfos();
            if (videoSectionInfos == null || videoSectionInfos.size()<= 0) {
                //没有视频返回空
                return null;
            }else {
                return videoSectionInfos.get(0).getLocalUrl();
            }
        }


}
